package com.unisyou.controller;

import com.github.binarywang.wxpay.bean.entpay.EntPayQueryResult;
import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.unisyou.utils.WXUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 企业付款相关接口
 */
@RequestMapping("/pay")
@RestController
public class EntPayController {
  private WxPayService wxService;

  @Autowired
  public EntPayController(WxPayService wxService) {
    this.wxService = wxService;
  }

  /**
   *企业付款到零钱
   * @param openId 用户openid
   */
  @PostMapping("/entPay")
  public EntPayResult entPay(String openId) throws WxPayException {
    EntPayRequest request = new EntPayRequest();
    request.setOpenid(openId);
    request.setPartnerTradeNo(WXUtils.getOrderId());
    request.setCheckName(WxPayConstants.CheckNameOption.NO_CHECK);
    request.setDescription("提现");
    request.setSpbillCreateIp(WXUtils.getLocalIP());
    return this.wxService.getEntPayService().entPay(request);
  }

  /**
   *查询企业付款到零钱的结果
   * @param partnerTradeNo 商户订单号
   */
  @GetMapping("/queryEntPay/{partnerTradeNo}")
  public EntPayQueryResult queryEntPay(@PathVariable String partnerTradeNo) throws WxPayException {
    return this.wxService.getEntPayService().queryEntPay(partnerTradeNo);
  }


  /**
   *获取RSA加密公钥
   * @return the public key
   * @throws WxPayException the wx pay exception
   */
  @GetMapping("/getPublicKey")
  public String getPublicKey() throws WxPayException {
    return this.wxService.getEntPayService().getPublicKey();
  }

}
